require 'kaixin'

class Kaixin_race < Kaixin
    def show_race(matchuid, matchid)
        begin
            @ie_for_kaixin.goto "http://www.kaixin001.com/app/app.php?aid=1040&url=match.php&matchuid=#{matchuid}&matchid=#{matchid}"
        rescue
            puts "Error for this match"
        end
    end
    
    def comeon_for(friendid)
        begin
            comeon = @ie_for_kaixin.link(:url, /#{friendid}/)
            comeon.click
        rescue
            puts "Error for this friend on the match"
        end
    end
end

require 'yaml'

if not File.exists?('match.yaml')
    puts "Please setup the match.yaml for configuration first"
    exit
end

puts "Loading configuration file match.yaml"
conf = YAML::load_file('match.yaml')
puts "Done of loading configuration file"

logins = conf["logins"]
info   = conf["matchinfo"]

matchid  = info["matchid"]
matchuid = info["matchuid"]
friendid = info["friendid"]

kaixin_race = Kaixin_race.new

puts "Start the IE"

logins.each do |login|
    puts "Processing with #{login['email']} .."
    kaixin_race.login_to_kaixin_with(login['email'], login['password'].to_s)
    kaixin_race.show_race(matchuid,matchid)
    kaixin_race.comeon_for(friendid)
    kaixin_race.logout
    puts "Done!!"
    sleep 5
end
puts "Done of the program!"
